// pages/complaint/complaint.js
// pages/euqip/equip.js
const {
  API,
  defaultPage,
  equipStatusEnum,
  CLINIC_MANAGER_CODE
} = require('../../utils/const.js');
const {
  formVaildate,
  checkCount
} = require('../../utils/formCheckUtil.js');
const {
  getRequest,
  postRequest
} = require('../../utils/request.js');
const re = require('../../utils/request.js');
const {
  EuipFormRoles
} = require('../../utils/rulse.js');
const {
  hasRole
} = require('../../utils/userUtil.js');
var app = getApp();

Page({
  data: {
    complaintTypes: [
      '医生额外开药、开检查',
      '医生态度不好',
      '手术事故',
      '未告知手术风险',
      '其他'
    ],
    departments: [
      '口腔科',
      '心脏内科',
      '神经内科'
    ],
    selectedComplaintType: '',
    selectedDepartment: '',
    complaintText: '',
    selectedDoctorName: '',
    selectedComplaintDoctor: {},
    complaintDoctors:[]
  },

   /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    const that = this;
    // 发起请求，获取数据
    getRequest(API.getDoctorList).then(res => {
      that.setData({
        complaintDoctors: res
      });
    });
  },

  onDoctorChange: function(e) {
    const index = e.detail.value; // 获取选中的索引
    const selectedDoctor = this.data.complaintDoctors[index]; // 根据索引获取选中的医生对象

    // 设置选中的医生对象和名字
    this.setData({
      selectedComplaintDoctor: selectedDoctor,
      selectedDoctorName: selectedDoctor.doctorName
    });

    console.log('选中的医生对象:', selectedDoctor);
  },

  onTypeChange(e) {
    this.setData({
      selectedComplaintType: this.data.complaintTypes[e.detail.value]
    });
  },

  onDepartmentChange(e) {
    this.setData({
      selectedDepartment: this.data.departments[e.detail.value]
    });
  },

  onInput(e) {
    this.setData({
      complaintText: e.detail.value
    });
  },

  onSubmit() {
    if (this.data.selectedComplaintType && this.data.complaintText) {
      // 提交表单数据
      console.log('投诉类型:', this.data.selectedComplaintType);
      console.log('投诉内容:', this.data.complaintText);
      // 这里可以添加实际提交的代码逻辑
      var user = wx.getStorageSync('userInfo');
      var patientId = user.userId;
      var formValue = {
        'patientId': patientId,
        'doctorId': this.data.selectedComplaintDoctor.doctorId,
        'department': this.data.selectedDepartment,
        'complaintType': this.data.selectedComplaintType,
        'complaintContent': this.data.complaintText,
        'complaintStatus': 'STARTED',
        'complaintTime': new Date().toISOString(),
        'pushTimes': 0
      };
      postRequest(API.addComplaint, formValue).then(res => {
        if (res) {
          wx.showToast({
            title: '提交成功，3-5日之内将会答复!',
            icon: 'success',
            duration: 2000,
            complete: function() {
              // 延时跳转，确保提示显示完毕
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/complaintList/complaintList'
                });
              }, 2000);  // 2秒钟后跳转
            }
          });
        }
      });
    }
  }
});